package com.woniuxy.factory.controller;

import com.woniuxy.factory.entity.Dep;
import com.woniuxy.factory.entity.DepDTO;
import com.woniuxy.factory.entity.Emp;
import com.woniuxy.factory.entity.ResponseResult;
import com.woniuxy.factory.mapper.PostMapper;
import com.woniuxy.factory.service.DepService;
import com.woniuxy.factory.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @NTLX data 2020 08 17
 */
@Controller
public class DepController {

    @Autowired
    DepService depService;
    @Autowired
    EmpService empService;
    @Autowired
    PostMapper postMapper;

    @RequestMapping("/dep/showAll.do")
    @ResponseBody
    public ResponseResult showAllDep() {
        //初始化对象
        ResponseResult<List> result = new ResponseResult<List>();
        result = depService.showAllDep();
        return result;
    }

    @RequestMapping("/dep/getId.do")
    @ResponseBody
    public ResponseResult getDepId(HttpServletRequest request, HttpServletResponse response) {
        //初始化对象
        ResponseResult<String> result = new ResponseResult<String>();
        //获取新部门编号并赋值给返回对象
        result.setData(depService.depId());
        return result;
    }

    @RequestMapping("/dep/add.do")
    @ResponseBody
    public ResponseResult addDep(Dep dep, String leader, @RequestParam("dleader") String[] dleader) {
        //初始化对象
        ResponseResult<String> result = new ResponseResult<>();
        //部门编号合法性验证
        if (dep.getDepId() == null || dep.getDepId().equals("")) {
            result.setCode(1);
            result.setData("无效的部门编号");
            return result;
        }
        //部门名称合法性验证
        if (dep.getDepName() == null || dep.getDepName().equals("")) {
            result.setCode(1);
            result.setData("无效的部门名称");
            return result;
        }
        try {
            //添加部门的事务
            result = depService.addDep(dep,leader,dleader);
        }catch (Exception e){
            //捕获异常的终止事务
            System.out.println(e.getMessage());
            result.setCode(1);
            result.setMsg(e.getMessage());
            result.setData(null);
            return result;
        }
        return result;
    }

    @RequestMapping("/dep/getDep.do")
    @ResponseBody
    public ResponseResult getDepInfo(String depId) {
        ResponseResult<DepDTO> result = new ResponseResult<>();
        Dep dep = depService.selectDepById(depId);
        if (dep == null) {
            result.setCode(1);
            result.setMsg("请勿篡改页面内容");
            return result;
        }
        result = depService.getDepInfo(depId);
        return result;
    }

    @RequestMapping("/dep/deleteDep.do")
    @ResponseBody
    public ResponseResult deleteDep(String depId) {
        ResponseResult result = new ResponseResult();
        System.out.println(depId);
        int a = depService.deleteDep(depId);
        if (a == 0) {
            result.setCode(1);
            result.setMsg("请勿篡改页面");
            return result;
        }
        result.setCode(0);
        result.setMsg("删除成功");
        return result;
    }

    @RequestMapping("/dep/edit.do")
    @ResponseBody
    public ResponseResult editDep(Dep dep, String leader, @RequestParam("dleader") String[] dleader){
        //初始化对象
        ResponseResult<String> result = new ResponseResult<>();
        //部门编号合法性验证
        if (dep.getDepId() == null || dep.getDepId().equals("")) {
            result.setCode(1);
            result.setData("无效的部门编号");
            return result;
        }
        //部门名称合法性验证
        if (dep.getDepName() == null || dep.getDepName().equals("")) {
            result.setCode(1);
            result.setData("无效的部门名称");
            return result;
        }
        try {
            //修改部门的事务
            result = depService.editDep(dep,leader,dleader);
        }catch (Exception e){
            //捕获异常的终止事务
            System.out.println(e.getMessage());
            result.setCode(1);
            result.setMsg(e.getMessage());
            result.setData(null);
            return result;
        }
        return result;
    }
}
